package jkd.lrn.net.ams.client;

import jkd.lrn.net.ams.core.message.MDRRequest;
import jkd.lrn.net.ams.core.message.MDRResponse;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class ClientSessionHandler implements IoHandler {

	private static final byte OK = 1;
	
	private final MDRRequest request;

	public ClientSessionHandler(MDRRequest request) {
		this.request = request;
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void messageReceived(IoSession session, Object pdu) throws Exception {
		
		final MDRResponse response = (MDRResponse) pdu;
		
		if(response.getStatus() == OK){
			System.out.println("MDR updated successfully.");
		} else {
			System.out.println("Failed to update MDR.");
		}
	}

	@Override
	public void messageSent(IoSession session, Object request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		
		System.out.println("About to write request ... " + request);
		session.write(request);
	}

}
